package cn.seaboot.admin.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 测试消息队列
 *
 * @author Mr.css
 * @version 2024-10-12 11:09
 */
public class TcpPublish {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";
    private static final String BROKER_URL = "tcp://localhost:61616";
    private static final String TOPIC_NAME = "MyQueue";

    public static void main(String[] args) throws Exception {
        // 连接池
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);

        // 连接
        Connection connection = factory.createConnection();
        connection.start();

        // 会话 - 自动确认 ACK
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createTopic(TOPIC_NAME);

        // 消息生产者 - 非持久化消息
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        TextMessage message = session.createTextMessage("Hello ActiveMQ!");
        producer.send(message);

        System.out.println("Sent message: " + message.getText());

        session.close();
        connection.close();
    }
}

